package com.link.cloud.config.mybatis.typeHandler;

import java.sql.CallableStatement;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;


/**
 * LocalDate与java.sql.Date数据类型转换
 * <br>
 * 自定义mybatis的数据类型转换
 * <br>
 * mybatis的配置时，设置TypeHandlersPackage
 * @author maxiaob
 *
 */
@MappedTypes(LocalDate.class)
public class LocalDateHandler extends BaseTypeHandler<LocalDate>{
	
	@Override
	public LocalDate getNullableResult(ResultSet rs, String cloumnName) throws SQLException {
		Date date = rs.getDate(cloumnName);
		if(date == null){
			return null;
		}
		return date.toLocalDate();
	}

	@Override
	public LocalDate getNullableResult(ResultSet rs, int cloumnIndex) throws SQLException {
		Date date = rs.getDate(cloumnIndex);
		if(date == null){
			return null;
		}
		return date.toLocalDate();
	}

	@Override
	public LocalDate getNullableResult(CallableStatement cs, int cloumnIndex) throws SQLException {
		Date date = cs.getDate(cloumnIndex);
		if(date == null){
			return null;
		}
		return date.toLocalDate();
	}

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, LocalDate localDate, JdbcType jdbcType)
			throws SQLException {
		ps.setDate(i, Date.valueOf(localDate));
	}

}
